home *** CD-ROM | disk | FTP | other *** search
/ Tech Arsenal 1 / Tech Arsenal (Arsenal Computer).ISO / tek-01 / ohlutil.zip / CMP.C < prev    next >
C/C++ Source or Header  |  1990-05-31  |  12KB  |  492 lines

  1. /* cmp -- compare two files.
  2.    Copyright (C) 1988, 1990 Free Software Foundation, Inc.
  3.  
  4.    This program is free software; you can redistribute it and/or modify
  5.    it under the terms of the GNU General Public License as published by
  6.    the Free Software Foundation; either version 1, or (at your option)
  7.    any later version.
  8.  
  9.    This program is distributed in the hope that it will be useful,
  10.    but WITHOUT ANY WARRANTY; without even the implied warranty of
  11.    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12.    GNU General Public License for more details.
  13.  
  14.    You should have received a copy of the GNU General Public License
  15.    along with this program; if not, write to the Free Software
  16.    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.  */
  17.  
  18. /* Differences from the Unix cmp:
  19.    * 6 - 40 - oo times faster.
  20.    * The file name `-' is always the standard input. If one file name
  21.      is omitted, the standard input is used as well.
  22.    * -q for fast compare.  Only prints the number of the first differing byte,
  23.      but not the line number.
  24.  
  25.    By Torbjorn Granlund, and David MacKenzie. */
  26.  
  27. #include <stdio.h>
  28. #include <errno.h>
  29. #include <sys/types.h>
  30. #include "system.h"
  31. #include "getopt.h"
  32.  
  33. #ifdef STDC_HEADERS
  34. #include <stdlib.h>
  35. #else
  36. char *malloc ();
  37.  
  38. extern int errno;
  39. #endif
  40.  
  41. #define max(h, i)    ((h) > (i) ? (h) : (i))
  42. #define min(l, o)    ((l) < (o) ? (l) : (o))
  43.  
  44. int bcmp_cnt ();
  45. int bcmp2 ();
  46. void cmp ();
  47. int bread ();
  48. void printc ();
  49. void error ();
  50.  
  51. /* Name under which this program was invoked.  */
  52.  
  53. char *program_name;
  54.  
  55. /* Filenames of the compared files.  */
  56.  
  57. char *file1;
  58. char *file2;
  59.  
  60. /* File descriptors of the files.  */
  61.  
  62. int file1_desc;
  63. int file2_desc;
  64.  
  65. /* Read buffers for the files.  */
  66.  
  67. char *buf1;
  68. char *buf2;
  69.  
  70. /* Optimal block size for the files.  */
  71.  
  72. int buf_size;
  73.  
  74. /* Output format:
  75.    0 to print the offset and line number of the first differing bytes
  76.    'l' to print the (decimal) offsets and (octal) values of all differing bytes
  77.    's' to only return an exit status indicating whether the files differ */
  78.  
  79. int flag = 0;
  80.  
  81. /* If nonzero, print values of bytes quoted like cat does. */
  82. int flag_print_chars = 0;
  83.  
  84. struct option long_options[] =
  85. {
  86.   {"silent", 0, &flag, 's'},
  87.   {"quiet", 0, &flag, 's'},
  88.   {"show-chars", 0, &flag, 'L'},
  89.   {"verbose", 0, &flag, 'l'},
  90.   {NULL, 0, NULL, 0}
  91. };
  92.  
  93. void
  94. usage (reason)
  95.      char *reason;
  96. {
  97.   if (reason != NULL)
  98.     fprintf (stderr, "%s: %s\n", program_name, reason);
  99.  
  100.   fprintf (stderr, "\
  101. Usage: %s [-lsL] [+verbose] [+silent] [+quiet] [+show-chars] file1 [file2]\n",
  102.        program_name);
  103.  
  104.   exit (2);
  105. }
  106.  
  107.  
  108. void
  109. main (argc, argv)
  110.      int argc;
  111.      char *argv[];
  112. {
  113.   int c;
  114.   struct stat stat_buf1, stat_buf2;
  115.   int ind = 0;
  116.  
  117.   program_name = *argv;
  118.  
  119.   /* If an argument is omitted, default to the standard input.  */
  120.  
  121.   file1 = "-";
  122.   file2 = "-";
  123.   file1_desc = fileno (stdin);
  124.   file2_desc = fileno (stdin);
  125.  
  126.   /* Parse command line options.  */
  127.  
  128.   while ((c = getopt_long (argc, argv, "lsL", long_options, &ind)) != EOF)
  129.     switch (c)
  130.       {
  131.       case 0:
  132.     break;
  133.       case 'l':
  134.       case 's':
  135.     flag = c;
  136.     break;
  137.       case 'L':
  138.     flag = 'l';
  139.     flag_print_chars = 1;
  140.     break;
  141.       default:
  142.     usage ((char *) 0);
  143.       }
  144.  
  145.   if (optind < argc)
  146.     file1 = argv[optind++];
  147.  
  148.   if (optind < argc)
  149.     file2 = argv[optind++];
  150.  
  151.   if (optind < argc)
  152.     usage ("extra arguments");
  153.  
  154.   if (strcmp (file1, "-"))
  155.     {
  156.       file1_desc = open (file1, O_RDONLY);
  157.       if (file1_desc < 0)
  158.     {
  159.       if (flag == 's')
  160.         exit (2);
  161.       else
  162.         error (2, errno, "%s", file1);
  163.     }
  164.     }
  165.  
  166.   if (strcmp (file2, "-"))
  167.     {
  168.       file2_desc = open (file2, O_RDONLY);
  169.       if (file2_desc < 0)
  170.     {
  171.       if (flag == 's')
  172.         exit (2);
  173.       else
  174.         error (2, errno, "%s", file2);
  175.     }
  176.     }
  177.  
  178.   if (file1_desc == file2_desc)
  179.     usage ("at least one filename should be specified");
  180.  
  181.   if (fstat (file1_desc, &stat_buf1) < 0)
  182.     error (2, errno, "%s", file1);
  183.   if (fstat (file2_desc, &stat_buf2) < 0)
  184.     error (2, errno, "%s", file2);
  185.  
  186.   /* If both the input descriptors are associated with plain files,
  187.      we can make the job simpler in some cases.  */
  188.  
  189.   if ((stat_buf1.st_mode & S_IFMT) == S_IFREG
  190.       && (stat_buf2.st_mode & S_IFMT) == S_IFREG)
  191.     {
  192.       /* Find out if the files are links to the same inode, and therefore
  193.      identical.  */
  194.  
  195.       if (stat_buf1.st_dev == stat_buf2.st_dev
  196.       && stat_buf1.st_ino == stat_buf2.st_ino)
  197.     exit (0);
  198.  
  199.       /* If output is redirected to "/dev/null", we may assume `-s'.  */
  200.  
  201.       if (flag != 's')
  202.     {
  203.       struct stat sb;
  204.       dev_t nulldev;
  205.       ino_t nullino;
  206.       if (stat ("/dev/null", &sb) == 0)
  207.         {
  208.           nulldev = sb.st_dev;
  209.           nullino = sb.st_ino;
  210.           if (fstat (1, &sb) == 0
  211.           && sb.st_dev == nulldev && sb.st_ino == nullino)
  212.         flag = 's';
  213.         }
  214.     }
  215.  
  216.       /* If only a return code is needed, conclude that
  217.      the files differ if they have different sizes.  */
  218.  
  219.       if (flag == 's' && stat_buf1.st_size != stat_buf2.st_size)
  220.     exit (1);
  221.     }
  222.  
  223.   /* Get the optimal block size of the files.  */
  224.  
  225.   buf_size = max (ST_BLKSIZE (stat_buf1), ST_BLKSIZE (stat_buf2));
  226.  
  227.   /* Allocate buffers, with space for sentinels at the end.  */
  228.  
  229.   buf1 = malloc (buf_size + sizeof (long));
  230.   buf2 = malloc (buf_size + sizeof (long));
  231.   if (buf1 == NULL || buf2 == NULL)
  232.     error (2, 0, "virtual memory exhausted");
  233.  
  234.   cmp ();
  235. }
  236.  
  237. /* Compare the two files already open.  */
  238.  
  239. void
  240. cmp ()
  241. {
  242.   long line_number = 1;        /* Line number (1...) of first difference. */
  243.   long char_number = 1;        /* Offset (1...) in files of 1st difference. */
  244.   int read1, read2;        /* Number of bytes read from each file. */
  245.   int first_diff;        /* Offset (0...) in buffers of 1st diff. */
  246.   int smaller;            /* The lesser of `read1' and `read2'. */
  247.   int exit_status = 0;
  248.  
  249.   do
  250.     {
  251.       read1 = bread (file1_desc, buf1, buf_size);
  252.       if (read1 < 0)
  253.     error (2, errno, "%s", file1);
  254.       read2 = bread (file2_desc, buf2, buf_size);
  255.       if (read2 < 0)
  256.     error (2, errno, "%s", file2);
  257.  
  258.       /* Insert sentinels for the block compare.  */
  259.  
  260.       buf1[read1] = ~buf2[read1];
  261.       buf2[read2] = ~buf1[read2];
  262.  
  263.       if (flag == 0)
  264.     {
  265.       int cnt;
  266.  
  267.       /* If the line number should be written for differing files,
  268.          compare the blocks and count the number of newlines
  269.          simultaneously.  */
  270.       first_diff = bcmp_cnt (&cnt, buf1, buf2, '\n');
  271.       line_number += cnt;
  272.     }
  273.       else
  274.     {
  275.       first_diff = bcmp2 (buf1, buf2);
  276.     }
  277.  
  278.       if (flag != 'l')
  279.     char_number += first_diff;
  280.       else
  281.     smaller = min (read1, read2);
  282.  
  283.       if (first_diff < read1 && first_diff < read2)
  284.     {
  285.       switch (flag)
  286.         {
  287.         case 0:
  288.           /* This format is a proposed POSIX standard.  */
  289.           printf ("%s %s differ: char %ld, line %ld\n",
  290.               file1, file2, char_number, line_number);
  291.           /* Fall through. */
  292.         case 's':
  293.           exit (1);
  294.         case 'l':
  295.           while (first_diff < smaller)
  296.         {
  297.           if (buf1[first_diff] != buf2[first_diff])
  298.             {
  299.               if (flag_print_chars)
  300.             {
  301.               printf ("%6ld", first_diff + char_number);
  302.               printf (" %3o ",
  303.                   (unsigned) (unsigned char) buf1[first_diff]);
  304.               printc (stdout,
  305.                   (unsigned) (unsigned char) buf1[first_diff]);
  306.               printf (" %3o ",
  307.                   (unsigned) (unsigned char) buf2[first_diff]);
  308.               printc (stdout,
  309.                   (unsigned) (unsigned char) buf2[first_diff]);
  310.               putchar ('\n');
  311.             }
  312.               else
  313.             /* This format is a proposed POSIX standard. */
  314.             printf ("%6ld %3o %3o\n",
  315.                 first_diff + char_number,
  316.                 (unsigned) (unsigned char) buf1[first_diff],
  317.                 (unsigned) (unsigned char) buf2[first_diff]);
  318.             }
  319.           first_diff++;
  320.         }
  321.           exit_status = 1;
  322.           break;
  323.         }
  324.     }
  325.  
  326.       if (flag == 'l')
  327.     char_number += smaller;
  328.  
  329.       if (read1 != read2)
  330.     {
  331.       switch (flag)
  332.         {
  333.         case 0:
  334.         case 'l':
  335.           /* This format is a proposed POSIX standard. */
  336.           printf ("%s: EOF on %s\n",
  337.               program_name, read1 < read2 ? file1 : file2);
  338.           break;
  339.         case 's':
  340.           break;
  341.         }
  342.       exit (1);
  343.     }
  344.     }
  345.   while (read1);
  346.   exit (exit_status);
  347. }
  348.  
  349. /* Compare two blocks of memory P1 and P2 until they differ,
  350.    and count the number of occurences of the character C in the common
  351.    part of P1 and P2.
  352.    Assumes that P1 and P2 are aligned at long addresses!
  353.    If the blocks are not guaranteed to be different, put sentinels at the ends
  354.    of the blocks before calling this function.
  355.    Return the offset of the first byte that differs.
  356.    Place the count at the address pointed to by COUNT.  */
  357.  
  358. int
  359. bcmp_cnt (count, p1, p2, c)
  360.      int *count;
  361.      char *p1, *p2;
  362.      unsigned char c;
  363. {
  364.   long w;            /* Word for counting C. */
  365.   long i1, i2;            /* One word from each buffer to compare. */
  366.   long *p1i, *p2i;        /* Pointers into each buffer. */
  367.   char *p1c, *p2c;        /* Pointers for finding exact address. */
  368.   unsigned int cnt = 0;        /* Number of occurrences of C. */
  369.   long cccc;            /* C, four times. */
  370.   long m0, m1, m2, m3;        /* Bitmasks for counting C. */
  371.  
  372.   cccc = ((long) c << 24) | ((long) c << 16) | ((long) c << 8) | ((long) c);
  373.  
  374.   m0 = 0xff;
  375.   m1 = 0xff00;
  376.   m2 = 0xff0000;
  377.   m3 = 0xff000000;
  378.  
  379.   p1i = (long *) p1;
  380.   p2i = (long *) p2;
  381.  
  382.   /* Find the rough position of the first difference by reading long ints,
  383.      not bytes.  */
  384.  
  385.   i1 = *p1i++;
  386.   i2 = *p2i++;
  387.   while (i1 == i2)
  388.     {
  389.       w = i1 ^ cccc;
  390.       cnt += (w & m0) == 0;
  391.       cnt += (w & m1) == 0;
  392.       cnt += (w & m2) == 0;
  393.       cnt += (w & m3) == 0;
  394.       i1 = *p1i++;
  395.       i2 = *p2i++;
  396.     }
  397.  
  398.   /* Find out the exact differing position (endianess independant).  */
  399.  
  400.   p1c = (char *) (p1i - 1);
  401.   p2c = (char *) (p2i - 1);
  402.   while (*p1c == *p2c)
  403.     {
  404.       cnt += c == *p1c;
  405.       p1c++;
  406.       p2c++;
  407.     }
  408.  
  409.   *count = cnt;
  410.   return p1c - p1;
  411. }
  412.  
  413. /* Compare two blocks of memory P1 and P2 until they differ.
  414.    Assumes that P1 and P2 are aligned at long addresses!
  415.    If the blocks are not guaranteed to be different, put sentinels at the ends
  416.    of the blocks before calling this function.
  417.    Return the offset of the first byte that differs.  */
  418.  
  419. int
  420. bcmp2 (p1, p2)
  421.      char *p1, *p2;
  422. {
  423.   long *i1, *i2;
  424.   char *c1, *c2;
  425.  
  426.   /* Find the rough position of the first difference by reading long ints,
  427.      not bytes.  */
  428.  
  429.   for (i1 = (long *) p1, i2 = (long *) p2; *i1++ == *i2++;)
  430.     ;
  431.  
  432.   /* Find out the exact differing position (endianess independant).  */
  433.  
  434.   for (c1 = (char *) (i1 - 1), c2 = (char *) (i2 - 1); *c1 == *c2; c1++, c2++)
  435.     ;
  436.  
  437.   return c1 - p1;
  438. }
  439.  
  440. /* Read NCHARS bytes from descriptor FD into BUF.
  441.    Return the number of characters successfully read.  */
  442.  
  443. int
  444. bread (fd, buf, nchars)
  445.      int fd;
  446.      char *buf;
  447.      int nchars;
  448. {
  449.   char *bp = buf;
  450.   int nread;
  451.  
  452.   for (;;)
  453.     {
  454.       nread = read (fd, bp, nchars);
  455.       if (nread < 0)
  456.     return -1;
  457.       bp += nread;
  458.       if (nread == nchars || nread == 0)
  459.     break;
  460.       nchars -= nread;
  461.     }
  462.   return bp - buf;
  463. }
  464.  
  465. /* Print character C on stream FS, making nonvisible characters
  466.    visible by quoting like cat does.  */
  467.  
  468. void
  469. printc (fs, c)
  470.      FILE *fs;
  471.      unsigned c;
  472. {
  473.   if (c >= 128)
  474.     {
  475.       putc ('M', fs);
  476.       putc ('-', fs);
  477.       c -= 128;
  478.     }
  479.   if (c < 32)
  480.     {
  481.       putc ('^', fs);
  482.       c += 64;
  483.     }
  484.   else if (c == 127)
  485.     {
  486.       putc ('^', fs);
  487.       c = '?';
  488.     }
  489.  
  490.   putc (c, fs);
  491. }
  492.